package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.BaseResp;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "编辑客户企业标签", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "userid", type = Type.String, description = "添加外部联系人的userid", required = true),
        @Parameter(name = "external_userid", type = Type.String, description = "外部联系人userid", required = true),
        @Parameter(name = "add_tag", type = Type.String, description = "要标记的标签列表", required = false),
        @Parameter(name = "remove_tag", type = Type.String, description = "要移除的标签列表", required = false),
})
@Component
public class MarkCorpTag implements Action {

    @Autowired
    private ExternalContactService externalContactService;

    @Override
    public Response execute(Request request) throws Exception {
        String userid = request.getString("userid");
        String external_userid = request.getString("external_userid");
        String add_tag = request.getString("add_tag");
        String remove_tag = request.getString("remove_tag");

        HashMap<String, Object> params = new HashMap<String, Object>();
        if (add_tag == null && remove_tag == null) {
            return Response.fail("参数错误！");
        }
        params.put("userid", userid);
        params.put("external_userid", external_userid);
        if (add_tag != null) {
            String[] addTagSplit = add_tag.split(",");
            params.put("group_id", addTagSplit);
        }
        if (remove_tag != null) {
            String[] removeTagSplit = remove_tag.split(",");
            params.put("remove_tag", removeTagSplit);
        }

        BaseResp rsp = externalContactService.markCorpTag(
                request.getString("access_token"),
                params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success();
    }
}
